<template>
  <div class="my-like-con">
    <div class="empty-con" v-if="isEmpry">
      <div class="empty-text">暂无喜欢</div>
    </div>
    <div v-else>
      <div class="type-choose">
        <div class="type-con" @click="typeVisible = true">类型选择</div>
      </div>
      <mt-actionsheet :actions="typeActions" v-model="typeVisible">
      </mt-actionsheet>
      <div class="empty-con" v-if="isEmpry2">
        <div class="empty-text">暂无喜欢</div>
      </div>
      <div v-else>
        <div class="block" v-for="(item, index) in reverseList" :key="index">
          <div class="like-detail" @click="toInfoDetail(item.work_id, item.type)">
            <div class="img-con">
              <img :src="circleUrl + item.img" width="100px" />
            </div>
            <div class="right-con">
              <div class="title">{{ item.title }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="price" v-if="item.price">￥{{ item.price }}</div>
            </div>
          </div>
          <div class="bottom">
            <div style="color: gray; line-height: 30px">
              {{ item.add_time | dataFormat }}
            </div>
            <div class="del" @click="delMyLike(item.id, item.title)">删除</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      circleUrl: "http://localhost:9528/admin/news_info/downloadPic?img_url=",
      isEmpry: false,
      isEmpry2: false,
      username: "",
      typeVisible: false,
      typeActions: [
        {
          id: 0,
          name: "全部",
          method: this.select,
        },
        {
          id: 1,
          name: "新闻类",
          method: this.select,
        },
        {
          id: 2,
          name: "图片类",
          method: this.select,
        },
        {
          id: 3,
          name: "商品类",
          method: this.select,
        },
      ],
      type: "",
      myLikeList: [],
    };
  },
  filters: {
    typeFormat(value) {
      switch (value) {
        case 1:
          return "新闻类";
        case 2:
          return "图片类";
        case 3:
          return "商品类";
        default:
          break;
      }
    },
  },
  created() {
    this.username = window.JSON.parse(
      localStorage.getItem("UserInfo")
    ).username;
  },
  activated() {
    this.getMyLike();
  },
  computed: {
    reverseList() {
      return this.myLikeList.reverse();
    },
  },
  methods: {
    getMyLike() {
      this.$http
        .get("/getMyLike", {
          params: {
            username: this.username,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            const data = res.data.message;
            if (data.length > 0) {
              this.myLikeList = data;
              this.isEmpry = false;
            } else {
              this.myLikeList = [];
              this.isEmpry = true;
            }
          } else {
            this.myLikeList = [];
            this.isEmpry = true;
          }
        });
    },
    // 选择类别
    select(item) {
      this.type = item.id;
      this.typeVisible = false;
      this.$http
        .get("/getMyLikeByType", {
          params: {
            username: this.username,
            type: this.type,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            const data = res.data.message;
            if (data.length > 0) {
              this.myLikeList = data;
              this.isEmpry2 = false;
            } else {
              this.myLikeList = [];
              this.isEmpry2 = true;
            }
          } else {
            this.myLikeList = [];
            this.isEmpry2 = true;
          }
        });
    },
    toInfoDetail(work_id, type) {
      // 首先判断是什么类别，然后跳到哪个区域去
      switch (type) {
        case 1:
          this.$router.push({
            path: "/idssp/news/newsinfo/" + work_id,
          });
          break;
        case 2:
          this.$router.push({
            path: "/idssp/photos/photoinfo/" + work_id,
          });
          break;
        case 3:
          this.$router.push({
            path: "/idssp/shops/goodsinfo/" + work_id,
          });
          break;
        default:
          break;
      }
    },
    delMyLike(id, title) {
      this.$messagebox
        .confirm("", {
          message: "是否从我的喜欢中删除" + title.substring(0, 6) + "...信息？",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
        .then((action) => {
          if (action == "confirm") {
            //确认的回调
            let params = {
              id: id,
            };
            this.$http.post("/delMyLike", params).then((res) => {
              if (res.data.errcode === 200) {
                Toast("删除成功!!!");
                this.getMyLike();
              } else {
                Toast("删除失败!!!");
              }
            });
          }
        })
        .catch((err) => {
          if (err == "cancel") {
            //取消的回调
          }
        });
    },
  },
};
</script>
<style lang='scss' scoped>
.my-like-con {
  font-size: 13px;
  background-color: #eee;
  overflow: hidden;
  padding: 10px 10px 0 10px;
  .empty-con {
    width: 200px;
    height: 200px;
    background: url("../../assets/img/order/empty.png") no-repeat;
    background-size: 100% 100%;
    background-position-y: center;
    margin: 0 auto;
    text-align: center;
    .empty-text {
      padding-top: 180px;
      font-size: 15px;
    }
  }
  .type-choose {
    // padding: 5px 0 5px 16px;
    padding-top: 10px;
    padding-left: 16px;
    width: 100%;
    // height: 10px;
    background-color: #fff;
    .type-con {
      width: 25%;
      text-align: center;
      line-height: 30px;
      border: 1px solid gray;
      background-color: #fff;
      border-radius: 15px;
    }
  }
  .block {
    // margin-bottom: 10px;
    padding: 10px 10px;
    background-color: #fff;
    .like-detail {
      display: flex;
      width: 100%;
      justify-content: space-between;
      .img-con {
      }
      .right-con {
        width: 60%;
        .title {
          font-size: 14px;
          font-weight: bold;
        }
        .price {
          margin-top: 5px;
          width: 55px;
          display: block;
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .content {
          // font-size: 13px;
          // text-align: left;
          margin-top: 5px;
          text-indent: 10px;
          white-space: nowrap; //不换行
          width: 200px; //宽度必须设置
          overflow: hidden;
          text-overflow: ellipsis; //溢出的文字使用...显示
        }
      }
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      .del {
        border: 1px solid #ccc;
        padding: 5px;
        border-radius: 20px;
        margin-right: 10px;
        width: 80px;
        height: 30px;
        text-align: center;
      }
    }
  }
}
</style>